package com.shujia.flink.window

import org.apache.flink.streaming.api.scala._
import org.apache.flink.streaming.api.windowing.assigners.{SlidingProcessingTimeWindows, TumblingEventTimeWindows, TumblingProcessingTimeWindows}
import org.apache.flink.streaming.api.windowing.time.Time

object Demo1TimeWindow {
  def main(args: Array[String]): Unit = {

    val env: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment


    val linesDS: DataStream[String] = env.socketTextStream("master", 8888)

    val kvDS: DataStream[(String, Int)] = linesDS.flatMap(_.split(",")).map((_, 1))


    /**
      * TumblingProcessingTimeWindows: 滚动（两个窗口没有交叉）处理时间窗口
      * TumblingEventTimeWindows：滚动的事件时间窗口，   需要设置时间字段和水位线
      *
      * SlidingProcessingTimeWindows: 滑动的处理时间窗口, 滑动窗口需要指定窗口大小和滑动时间
      * SlidingEventTimeWindows：滑动的事件时间窗口
      */
    kvDS
      .keyBy(_._1)
      //.timeWindow(Time.seconds(5))
      .window(SlidingProcessingTimeWindows.of(Time.seconds(15), Time.seconds(5)))
      .sum(1)
      .print()

    env.execute()

  }

}
